package fi.joensuu.mpm;

import android.app.Activity;
import android.widget.Toast;

public class Utils {
	
	/**
	 * Shows first line that has line number from stackTrace
	 * @param t exception instance
	 * @param sourceActivity source activity
	 */
	public static void displayStackTrace(Throwable t, Activity sourceActivity) {
		displayStackTrace(t,sourceActivity,1);
	}
		
	public static void displayStackTrace(Throwable t, Activity sourceActivity, int howMany) {
		
		//try to get first line with line number		
		StackTraceElement[] stackTrace = t.getStackTrace();
		int index = 0;//first line with line number
		for (StackTraceElement st : stackTrace){
			if (st.getLineNumber()>0){								
				break;				
			}
			index++;
		}					
		
		if (howMany == -1){
			howMany = stackTrace.length;
		}
		
		//display that line
		for (int i = 0; i<100; i++){
			Toast.makeText(sourceActivity,
					""+(i%howMany+index)+". "+t.getClass()+": "+t.getMessage()+
					"\n"+stackTrace[index+i%howMany],
					Toast.LENGTH_LONG).show();
		}	
	}

	public static void showMessage(String text, Activity sourceActivity) {
		Toast.makeText(sourceActivity,text, Toast.LENGTH_LONG).show();
		
	}

	public static void showShortMessage(String text, Activity sourceActivity) {
		Toast.makeText(sourceActivity,text, Toast.LENGTH_SHORT).show();
		
	}

	/*I tak nie dziala to tak jak trzeba
	 * public static float[] calculateEqualWeights(TextView ... horizontalTextViews) {
	 *
		int[] lengths = new int[horizontalTextViews.length];
		int i =0;
		for (TextView txtView: horizontalTextViews){
			String label = txtView.getText().toString();
			String labelParts[] = label.split("\\n");
			int maxLenght = labelParts[0].length();
			for (int j=1;j<labelParts.length; j++){
				if (labelParts[j].length()>maxLenght){
					maxLenght=labelParts[j].length();
				}
			}
			lengths[i]=maxLenght;
			i++;
		}
		int maxGlobalLength = lengths[0];
		for (int j=0;j<lengths.length;j++){
			if (lengths[j]>maxGlobalLength){
				maxGlobalLength =lengths[j]; 
			}
		}
		float[] weights = new float[horizontalTextViews.length];
		for (int k=0; k<lengths.length;k++){
			weights[k] = lengths[k]/maxGlobalLength;			
		}
		return weights;
	}*/
}
